package com.it.system.service.impl;


import com.github.pagehelper.PageHelper;
import com.it.system.domain.Role;
import com.it.system.mapper.RoleMapper;
import com.it.system.query.RoleQuery;
import com.it.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
// 查询事务控制方式：设置只读 支持当前事务，如果没有当前事务，就以非事务方法执行。
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void addRole(Role role) {
        roleMapper.addRole(role);

        if (role.getPermissionids().size()>0){
            roleMapper.addRoleAndPs(role);
        }

        if (role.getMenuids().size()>0){
            roleMapper.addRoleAndMs(role);
        }
    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        roleMapper.updateRole(role);

        roleMapper.delRoleAndPs(role.getId());
        roleMapper.delRoleAndMs(role.getId());
        System.out.println(role.getPermissionids());
        System.out.println(role.getMenuids());
        if (role.getPermissionids().size()>0){
            roleMapper.addRoleAndPs(role);
        }

        if (role.getMenuids().size()>0){
            roleMapper.addRoleAndMs(role);
        }
    }

    @Override
    @Transactional
    public void deteleById(Long id) {
        roleMapper.deteleById(id);

        roleMapper.delRoleAndPs(id);
        roleMapper.delRoleAndMs(id);
    }

    @Override
    @Transactional
    public void deleteRoleByIds(Long[] ids) {
        roleMapper.deleteRoleByIds(ids);

        for (Long id : ids) {
            roleMapper.delRoleAndPs(id);
            roleMapper.delRoleAndMs(id);
        }
    }

    @Override
    public Role findOne(Long id) {
        return roleMapper.findOne(id);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public List<Role> queryPage(RoleQuery roleQuery) {
        //开始设置分页信息
        //干了三件事情：
        //1.查询总条数 2.根据驱动名称自动添加分页 3.分页查询返回结果
        PageHelper.startPage(roleQuery.getCurrentPage(), roleQuery.getPageSize());
        //直接高级查询，自动实现分页
        List<Role> roles = roleMapper.queryPage(roleQuery);//ctr 1

        //将roles中的id遍历取出放入集合中
        List<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());

        //通过roleIds集合查询出每个id对应的权限封装成Role集合对象
        List<Role> rolePersList=roleMapper.findPersByRoleIds(roleIds);
        List<Role> roleMenusList=roleMapper.findMenusByRoleIds(roleIds);

        //将角色id权限集合对象通过stream流将其转化成键为roleId值为Role的map，其中Role里包含了其对应的权限集合
        Map<Long, Role> roleIdPersMap = rolePersList.stream().collect(Collectors.toMap(Role::getId, r -> r));
        Map<Long, Role> roleIdMenusMap = roleMenusList.stream().collect(Collectors.toMap(Role::getId, r -> r));

        //遍历roles，跟着id在map里查询出Role对象
        for (Role role : roles) {
            Role rolePerms = roleIdPersMap.get(role.getId());
            //判断查询出Role是否为空，不为空就从map集合的Role对象中取出权限集合放入role里
            if (rolePerms != null) {
                role.setPermissionids(rolePerms.getPermissionids());
            }
            Role roleMenus = roleIdMenusMap.get(role.getId());
            if (roleMenus != null) {
                role.setMenuids(roleMenus.getMenuids());
            }
        }





//        for (Role role : roles) {
//            List<Long> psList=roleMapper.findPs(role.getId());
//            role.setPermissionids(psList);
//            List<Long> msList=roleMapper.findMs(role.getId());
//            role.setMenuids(msList);
//        }
        return roles;
    }
}
